package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.PromotionStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgProductPackMapper;
import com.ygqh.baby.model.YgProduct4ListModel;
import com.ygqh.baby.model.YgProductPackModel;
import com.ygqh.baby.po.YgProduct;
import com.ygqh.baby.po.YgProductPack;
import com.ygqh.baby.po.YgProductPackExample;
import com.ygqh.baby.service.YgProductPackService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 请使用 YgProductPackV2ServiceImpl
 */
@Deprecated
@Service
public class YgProductPackServiceImpl implements YgProductPackService {

	@Autowired
	private YgProductPackMapper ygProductPackMapper;

	@Override
	public List<YgProductPack> find() {
		YgProductPackExample example = new YgProductPackExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygProductPackMapper.selectByExample(example);
	}

	@Override
	public YgProductPack findValidById(Long packId) {
		YgProductPackExample example = new YgProductPackExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name()).andIdEqualTo(packId);
		List<YgProductPack> list = ygProductPackMapper.selectByExample(example);
		if (list.isEmpty()) {
			return null;
		} else {
			return list.get(0);
		}
	}

	@Override
	public int findCount() {
		YgProductPackExample example = new YgProductPackExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygProductPackMapper.countByExample(example);
	}

	@Override
	public YgProductPack findById(Long id) {
		return ygProductPackMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgProductPack ygProductPack) {
		return ygProductPackMapper.insertSelective(ygProductPack);
	}

	@Override
	public int update(YgProductPack ygProductPack) {
		return ygProductPackMapper.updateByPrimaryKeySelective(ygProductPack);
	}

	@Override
	public ResultSet<YgProductPack> search(QueryInfo queryInfo, Long supplierId, PromotionStatus pStatus, Date startDate, Date endDate, String q) {
		List<YgProductPack> list = ygProductPackMapper.selectSuper(queryInfo, supplierId, pStatus == null ? null : pStatus.name(), startDate, endDate, q);
		int count = ygProductPackMapper.countSuper(supplierId, pStatus == null ? null : pStatus.name(), startDate, endDate, q);

		ResultSet<YgProductPack> resultSet = new ResultSet<YgProductPack>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgProductPackExample example = new YgProductPackExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgProductPack record = new YgProductPack();
		record.setStatus(DataStatus.Delete);

		return ygProductPackMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgProductPack ygProductPack) {
		int r = 0;
		if (ygProductPack.getId() != null) {
			r = this.update(ygProductPack);
		} else {
			r = this.save(ygProductPack);
		}
		return r;
	}

	@Override
	public ResultSet<YgProduct4ListModel> findProductByPackId(QueryInfo queryInfo, Long packId) {
		if(queryInfo==null){
			 queryInfo = new QueryInfo();
			queryInfo.setEnd(200);
		}	
		List<YgProduct4ListModel> list = ygProductPackMapper.selectProductByPackId(queryInfo, packId);
		int count=ygProductPackMapper.selectProductCount(packId);
		ResultSet<YgProduct4ListModel> resultSet = new ResultSet<YgProduct4ListModel>(count, list);
		return resultSet;
		
	}

	@Override
	public List<YgProduct> findProductForProductPack(QueryInfo queryInfo, String productName, Long groupId, String productStatus, Long supplierId, Long packId) {
		List<YgProduct> list = ygProductPackMapper.selectProductListToPack(queryInfo, productName, groupId, productStatus, supplierId, packId);

		return list;
	}

	@Override
	public List<YgProductPackModel> findOwnProductPackList(Long productId) {
		return ygProductPackMapper.selectOwnProductPackList(productId);
	}

	@Override
	public List<YgProductPack> findProductPackList() {
		return ygProductPackMapper.selectProductPackList();
	}

	@Override
	public List<YgProduct> findNewCustomByProductId(String productIds) {
		return ygProductPackMapper.selectProductName(productIds);
	}

	@Override
	public YgProductPack findPackByProductId(Long productId) {
		return ygProductPackMapper.selectPackByProductId(productId);
	}

	@Override
	public List<YgProductPack> findNotCompleteAction(Long packId, Date startDate) {
		return ygProductPackMapper.selectNotCompleteAction(packId, startDate);
	}

	@Override
	public List<YgProduct> findProductByBack(Long packId) {
		return ygProductPackMapper.selectProductByPack(packId);
	}

}
